package com.yidu.logisticssystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.logisticssystem.entity.LogisticsCardriver;
import com.yidu.logisticssystem.entity.LogisticsMap;
import com.yidu.logisticssystem.entity.LogisticsStaff;
import com.yidu.logisticssystem.entity.vo.CarDriverVo;
import com.yidu.logisticssystem.service.LogisticsCardriverService;
import com.yidu.logisticssystem.service.LogisticsMapService;
import com.yidu.logisticssystem.utils.BaseMessage;
import com.yidu.logisticssystem.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (LogisticsCardriver)表控制层
 *
 * @author Aaron
 * @since 2021-03-17 09:49:49
 */
@Api(tags = "司机表")
@RestController
@RequestMapping("logisticsCardriver")
public class LogisticsCardriverController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsCardriverService logisticsCardriverService;

    @Resource
    private LogisticsMapService logisticsMapService;


    /**
     * 查询所有车辆信息
     *
     * @param carDriverVo
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询所有司机信息")
    @GetMapping("selectAll")
    public Map<String, Object> findAll(CarDriverVo carDriverVo, int page, int limit) {
        //将参数传入到分页助手
        PageHelper.startPage(page, limit);
        //new一个map集合
        Map<String, Object> map = new HashMap<>();
        //调用service的查询所有司机的方法，用list集合接收
        List<CarDriverVo> list = logisticsCardriverService.queryAll(carDriverVo);
        //查询到所有司机之后，将集合数据封装到分页助手的PageInfo对象中
        PageInfo pageInfo = new PageInfo(list);
        //定义空字符串str,先得到地区信息
        String str = "";
        //循环得到所有的司机，list
        for (CarDriverVo car : list) {
            System.out.println("此行数据" + car.getMapId());
            //判断司机id的长度==2
            if (car.getMapId().length() == 2) {
                //调用地区表，查询该id对应的城市
                LogisticsMap logisticsMap = logisticsMapService.queryById(car.getMapId());
                //将查到的城市给到str
                str = logisticsMap.getMapName();
                //判断司机id的长度==3
            } else if (car.getMapId().length() == 3) {
                //调用地区表，查询该id对应的城市的上级，用集合接收
                List<LogisticsMap> logisticsMapList = logisticsMapService.selectfufind(car.getMapId());
                //调用地区表，查询该id对应的城市
                LogisticsMap logisticsMap2 = logisticsMapService.queryById(car.getMapId());
                //循环上级城市的信息
                for (LogisticsMap s : logisticsMapList) {
                    //得到城市的上级名称
                    str = s.getMapName();
                    //得到当前城市的名称
                    str += logisticsMap2.getMapName();
                }
                //判断当id大于3
            } else if (car.getMapId().length() > 3) {
                //调用地区表，查询该id对应的城市的上级，用集合接收
                List<LogisticsMap> logisticsMapList = logisticsMapService.selectfufind(car.getMapId());
                //调用地区表，查询该id对应的城市
                LogisticsMap logisticsMap2 = logisticsMapService.queryById(car.getMapId());
                //循环
                for (LogisticsMap s : logisticsMapList) {
                    //得到logisticsMapList集合的id，并再次得到他的上级
                    List<LogisticsMap> logisticsMapList1 = logisticsMapService.selectfufind(s.getMapId());
                    //循环
                    for (LogisticsMap s1 : logisticsMapList1) {
                        //得到最上级城市的名称 kkk
                        str = s1.getMapName();
                        //得到次上级城市的名称
                        str += s.getMapName();
                        //得到当前城市的名称
                        str += logisticsMap2.getMapName();
                    }
                }
            }
            //赋值
            car.setMapName(str);
        }
        //查询行数
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", pageInfo.getList()); // 这里可以直接写list
        map.put("count", pageInfo.getTotal()); // 总行数自动生成
        return map;
        //return Tools.getLayUiMap2(list,page,limit);
    }

    /**
     * 添加或者修改
     *
     * @param driverId
     * @param logisticsCardriver
     * @return
     */
    @ApiOperation(value = "添加或修改")
    @PostMapping("addOrUpdate")
    public BaseMessage addOrUpdate(Integer driverId, LogisticsCardriver logisticsCardriver) {
        //判断carId是否为空
        if (driverId == null) {
            //调用添加方法
            logisticsCardriverService.insertSelective(logisticsCardriver);
        } else {
            //否则调用修改方法
            logisticsCardriverService.update(logisticsCardriver);
        }
        return BaseMessage.succes();
    }

    /**
     * 修改赋值
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "修改赋值，根据Id查询")
    @PostMapping("update")
    public BaseMessage update(Integer id) {
        LogisticsCardriver car = logisticsCardriverService.queryById(id);
        return BaseMessage.succes(car);
    }


    /**
     * 删除
     *
     * @param carId
     * @return
     */
    @ApiOperation(value = "根据Id删除司机信息")
    @PostMapping("delete")
    public BaseMessage delete(Integer carId) {
        return logisticsCardriverService.deleteById(carId) > 0 ?
                BaseMessage.succes() : BaseMessage.fail();
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据Id批量删除，司机信息")
    @PostMapping("deleteAll")
    public BaseMessage deleteAll(String ids) {
        return logisticsCardriverService.deleteAll(ids) > 0 ?
                BaseMessage.succes() : BaseMessage.fail();
    }


    /**
     * 通过司机当前所在地查询单条数据
     * @return 单条数据
     */
    @ApiOperation(value = "根据mapId，查询空闲司机")
    @GetMapping("queryMapId")
    public Map<String, Object> queryMapId(HttpSession session) {
        //员工表，得到登录进来人员的地址
        LogisticsStaff logisticsStaff = (LogisticsStaff)session.getAttribute("staff");
        //查询空闲的司机
        List<CarDriverVo> list=logisticsCardriverService.queryMapId(logisticsStaff.getMapId());
        return Tools.getLayUiMap(list);
    }

}
